package cn.com.wangluotongxin.merchants.controller;

import cn.com.wangluotongxin.common.web.AjaxResult;
import cn.com.wangluotongxin.db.pojo.PageData;
import cn.com.wangluotongxin.merchants.dto.GoodsStandardDto;
import cn.com.wangluotongxin.merchants.form.GoodsStandardEditForm;
import cn.com.wangluotongxin.merchants.form.GoodsStandardForm;
import cn.com.wangluotongxin.merchants.service.IMerchantsGoodsStandardService;
import cn.com.wangluotongxin.merchants.vo.GoodsStandardVo;
import cn.com.wangluotongxin.web.utils.TenantAuthUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 商户管理-商品管理-商品规格 前端控制器
 * </p>
 *
 * @author AutoGenerator
 * @since 2024-06-20
 */
@Api(tags = "商户管理-商品管理-商品规格 相关接口")
@RestController
@RequestMapping("/goods/standard")
public class MerchantsGoodsStandardController {

    @Autowired
    private IMerchantsGoodsStandardService goodsStandardService;

    @ApiOperation("分页")
    @GetMapping("/page")
    public AjaxResult<PageData<GoodsStandardVo>> getPage(GoodsStandardDto dto){
        IPage<GoodsStandardVo> page = goodsStandardService.getPage(dto);
        return AjaxResult.success(PageData.build(page));
    }

    @ApiOperation("详情")
    @GetMapping("/detail/{id}")
    public AjaxResult<GoodsStandardVo> detail(@PathVariable("id") Integer id) {
        return AjaxResult.success(goodsStandardService.detail(id));
    }

    @ApiOperation("新增")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody GoodsStandardForm form) {
        Assert.notNull(form.getGoodsId(), "缺少商品ID");
        Assert.isTrue(StrUtil.isNotBlank(form.getStandardName()), "缺少规格名称");
        Assert.isTrue(StrUtil.isNotBlank(form.getStandardImages()), "缺少图片");

        goodsStandardService.add(form, TenantAuthUtil.getTenantUser());
        return AjaxResult.success();
    }

    @ApiOperation("编辑")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody GoodsStandardEditForm form) {
        Assert.notNull(form.getId(), "缺少数据ID");
        Assert.notNull(form.getGoodsId(), "缺少商品ID");
        Assert.isTrue(StrUtil.isNotBlank(form.getStandardName()), "缺少规格名称");
        Assert.isTrue(StrUtil.isNotBlank(form.getStandardImages()), "缺少图片");

        goodsStandardService.edit(form, TenantAuthUtil.getTenantUser());
        return AjaxResult.success();
    }

    @ApiOperation("上架/下架")
    @ApiImplicitParams({
            @ApiImplicitParam(name="isSale", value = "0.下架 1.上架")
    })
    @PostMapping("/sale/{id}/{isSale}")
    public AjaxResult sale(@PathVariable("id") Integer id, @PathVariable("isSale") Integer isSale) {

        goodsStandardService.sale(id, isSale, TenantAuthUtil.getTenantUser());
        return AjaxResult.success();
    }
}